import { Context, Application } from 'egg'

export default (app: Application) => {
  app.logger.info('egg-socket.io middleware app/io/middleware/connection.js')

  // 踢出用户示例
  // const tick = (id, msg) => {
  //   logger.debug('#tick', id, msg)
  //   socket.emit(id, msg)
  //   app.io.of('/').adapter.remoteDisconnect(id, true, err => {
  //     logger.error(err)
  //   })
  // }

  // const tickCurrent = async ctx => {
  //   ctx.socket.disconnet()
  // }

  return async (ctx: Context, next: () => Promise<any>) => {
    // ctx.logger.info('egg-socket.io middleware connection', ctx.socket.nsp.name, ctx.socket.id)
    // ctx.socket.emit('res', 'connected!')

    // const tick = false
    // if (tick) {
    //   tickCurrent(ctx)
    //   return
    // }
    await next()

    // execute when disconnect.
    // ctx.logger.info('disconnection!')
    // ctx.logger.info('egg-socket.io middleware connection done')
  }
}
